//
//  UIBarButtonItem+Item.m
//  baisibudejie
//
//  Created by 陈博文 on 15/12/18.
//  Copyright © 2015年 陈博文. All rights reserved.
//

#import "UIBarButtonItem+Item.h"

@implementation UIBarButtonItem (Item)


+(UIBarButtonItem *)barButtonItemWithImage:(UIImage *)image HighLightImage:(UIImage *)highLightImage Target:(id)target Action:(SEL)action{
    
    
    //需要设置两种状态
    UIButton *button = [[UIButton alloc]init];
    [button setImage:image forState:UIControlStateNormal];
    
    [button setImage:highLightImage forState:UIControlStateHighlighted];
    
    //设置自适应尺寸
    [button sizeToFit];
    //    需要设置 button 的点击时间,需要传入两个参数
    //注意点,不能使用
//    [button addTarget:target action:@selector(action) forControlEvents:UIControlEventTouchUpInside];

    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
   
    
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    
    return barButtonItem;

    
}

+(UIBarButtonItem *)barButtonItemWithImage:(UIImage *)image SelectedImage:(UIImage *)selectedImage Target:(id)target Action:(SEL)action{
    
    
    //需要设置两种状态
    UIButton *button = [[UIButton alloc]init];
    [button setImage:image forState:UIControlStateNormal];
    
    [button setImage:selectedImage forState:UIControlStateSelected];
    
    //设置自适应尺寸
    [button sizeToFit];
    //    需要设置 button 的点击时间,需要传入两个参数
    //注意点,不能使用
    //    [button addTarget:target action:@selector(action) forControlEvents:UIControlEventTouchUpInside];
    
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    
    return barButtonItem;
    
    
}


+ (UIBarButtonItem *)backItemWithImage:(UIImage *)image highImage:(UIImage *)highImage target:(nullable id)target action:(nullable SEL)action norColor:(UIColor *)norColor highColor:(UIColor *)highColor title:(NSString *)title
{
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    // 设置标题
    [backButton setTitle:title forState:UIControlStateNormal];
    
    // 设置图片
    [backButton setImage:image forState:UIControlStateNormal];
    [backButton setImage:highImage forState:UIControlStateHighlighted];
    
    // 设置标题颜色
    [backButton setTitleColor:norColor forState:UIControlStateNormal];
    [backButton setTitleColor:highColor forState:UIControlStateHighlighted];
    
    // 自适应尺寸:自动根据按钮图片和文字计算按钮大小
    [backButton sizeToFit];
    [backButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    // 设置按钮内容内边距
    backButton.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    
    return  [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
}


@end
